package cn.springcloud.fix.auth.social.wechat;

import cn.springcloud.fix.auth.social.wechat.connect.WechatConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactory;

@Configuration
@ConditionalOnProperty(prefix = "ssb.security.social.wechat", name = "app-id")
@Order(2)
public class WechatAutoConfig extends SocialConfigurerAdapter {

    @Value("${ssb.security.social.wechat.app-id}")
    private String appId;

    @Value("${ssb.security.social.wechat.app-secret}")
    private String appSecret;

    @Value("${ssb.security.social.wechat.provider-id}")
    private String providerId;

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer configurer, Environment environment) {
        configurer.addConnectionFactory(createConnectionFactory());
    }

    private ConnectionFactory<?> createConnectionFactory() {
        return new WechatConnectionFactory(providerId, appId, appSecret);
    }
}
